
import time
import sendDing
import cy_request
import cy_config
import cy_operate
import cy_log
import os

# 发送时间
lsat_send_time = cy_operate.getCurrentTime()
start_send_time = cy_operate.getCurrentTime()

lsat_report_time = cy_operate.getCurrentTime()

# 得到价格信息
def getBlanAbout():

    result = cy_request.getBalances()

    if result and len(result):

        blan_item = result[0]

        for c_blan_item in result:

            time.sleep(cy_config.min_sleep_amount)

            ccy = c_blan_item['ccy']

            if 'USDT' in ccy:

                blan_item = c_blan_item

        # 真实价值
        balance = blan_item['eq']
        balance = round(float(balance), 2)
        balance = str(balance)

        # 可用资产
        avaPrice = blan_item['availEq']
        avaPrice = round(float(avaPrice), 2)
        avaPrice = str(avaPrice)

        # 总价值
        cashBal = blan_item['cashBal']
        cashBal = round(float(cashBal), 2)
        cashBal = str(cashBal)

        total_price_str = '当前总金额：' + cashBal + ' 当前可用金额：' + avaPrice
        end_price_str = '资产估值：' + str(balance)

        return total_price_str + '\n' + end_price_str
    
    return ''

# 得到持仓中的信息
def getRiskAboutMessage(c_positionSide):

    postion_item = {
        'risk_total_str' : '持仓为空',
        'roe_price' : 0,
        'symbol_amout' : 0,
        'total_price' : 0,
        'can_used_price' : 0,
        'org_price' : 0
    }

    # 加载持仓信息
    cy_request.initMyRisk()

    local_symbol = cy_config.symbol

    if '-' in local_symbol:
        pass
    else:
        local_symbol = local_symbol.replace('USDT', '-USDT')

    # 获取一下当前币种持仓信息
    for risk in cy_request.my_postion_risks:

        time.sleep(cy_config.min_sleep_amount)

        symbol_str = risk['instId']

        # 持仓方向
        positionSide = risk['posSide']

        if (local_symbol in symbol_str) and positionSide == c_positionSide:

            # 持仓数量
            quantity = risk['pos']
            quantity = abs(int(quantity))
            quantity = str(quantity)

            # 持仓未实现盈亏
            unRealizedProfit = risk['upl']
            unRealizedProfit = round(float(unRealizedProfit), 2)
            unRealizedProfit = str(unRealizedProfit)

            chican_str = '持仓数量：' + quantity + ' 持仓未实现盈亏：' + unRealizedProfit + ' 持仓方向：' + positionSide

            postion_item['roe_price'] = unRealizedProfit
            postion_item['symbol_amout'] = quantity

            risk_total_str = chican_str

            postion_item['risk_total_str'] = risk_total_str

    return postion_item

def getMoreAbout(start_time, lsat_send_time, tao_dan_amount, trad_amount):
    # 开多
    # 通过历史成交得到收益
    yk_postion_item = cy_operate.getSymbolYK(cy_config.symbol, start_time, lsat_send_time, True)
    more_total_profit = yk_postion_item['run_time_desc']

    more_total_profit = more_total_profit + ' 套单数：' + str(tao_dan_amount) + ' 成交单：' + str(trad_amount) + ' 最大成交单：' + str(cy_config.max_more_trad_amount)

    shouyi_str = '币种：' + cy_config.symbol + ' ' + more_total_profit

    risk_total_item = getRiskAboutMessage('long')
    risk_total_str = risk_total_item['risk_total_str']
    
    blanceStr = getBlanAbout()

    sendDing.send_message( '\n' + shouyi_str + '\n' + risk_total_str + '\n' + blanceStr )

def getLessAbout(start_time, lsat_send_time, tao_dan_amount, trad_amount):

    # 开空
    yk_postion_item = cy_operate.getSymbolYK(cy_config.symbol, start_time, lsat_send_time, False)
    less_total_profit = yk_postion_item['run_time_desc']

    less_total_profit = less_total_profit + ' 套单数：' + str(tao_dan_amount) + ' 成交单：' + str(trad_amount) + ' 最大成交单：' + str(cy_config.max_less_trad_amount)

    shouyi_str = '币种：' + cy_config.symbol + ' ' + str(less_total_profit)

    risk_total_item = getRiskAboutMessage('short')
    risk_total_str = risk_total_item['risk_total_str']

    blanceStr = getBlanAbout()

    sendDing.send_message( '\n' + shouyi_str + '\n' + risk_total_str + '\n' + blanceStr )

def open(tao_dan_amount, trad_amount, isMore):

    # 60走一次
    global lsat_send_time

    n_time = cy_operate.getCurrentTime()
    cha = int(n_time) - int(lsat_send_time)
    cha = cha / 1000

    if cha > 60:

        # 设置最后一次发送为当前
        lsat_send_time = cy_operate.getCurrentTime()

        # 执行发送
        try:
            # 先获取区间管理对象
            if isMore:
                getMoreAbout(start_send_time, lsat_send_time, tao_dan_amount, trad_amount)
            else:
                getLessAbout(start_send_time, lsat_send_time, tao_dan_amount, trad_amount)

        except Exception as e:
            cy_log.printAndInLog_profit('=========== 报错 ============')
            cy_log.printAndInLog_profit('收益线程报错信息：' + str(e))
            cy_log.printAndInLog_profit('=========== 报错 ============')
        else:
            pass